package com.ziroom.crm.mq.core.processor.retry

import com.ziroom.crm.mq.core.BaseSpecification
import com.ziroom.crm.mq.core.MqType
import com.ziroom.crm.mq.core.config.MqConfig
import com.ziroom.crm.mq.core.context.InvokeContext
import com.ziroom.crm.mq.core.data.Message
import com.ziroom.crm.mq.core.node.StatisticNode
import com.ziroom.crm.mq.core.processor.QueueWrapper
import com.ziroom.crm.mq.core.retry.RetryConst

import java.time.LocalDateTime
import java.util.function.Consumer

class DefaultConsumeRetryProcessorTest extends BaseSpecification {
    private InvokeContext invokerContent
    private QueueWrapper queueWrapper


    def setup() {
        def message = new Message(UUID.randomUUID().toString(), "message body")
        def node = new StatisticNode()
        invokerContent = new InvokeContext(message, node)
        def callback = { println(LocalDateTime.now().toString()+":重试方法回调：打印：" + it) } as Consumer
        queueWrapper = new QueueWrapper("my.queue.name", MqType.RABBIT, callback)
    }

    void testPreConsumeWithDefaultConfigSuccess() {
        // 因为spi 也是通过默认构造器加载类的，所以可以通过new 的方式来创建实例
        DefaultConsumeRetryProcessor consumeRetryProcessor = new DefaultConsumeRetryProcessor()
        consumeRetryProcessor.preConsume(invokerContent, queueWrapper)
        expect:
        1==1
    }

    void testPostConsumeWithDefaultConfigSuccess() {
        // 因为spi 也是通过默认构造器加载类的，所以可以通过new 的方式来创建实例
        DefaultConsumeRetryProcessor consumeRetryProcessor = new DefaultConsumeRetryProcessor()
        consumeRetryProcessor.postConsume(invokerContent, queueWrapper)
        expect:
        1==1
    }

    void testPostConsumeWithDefaultConfigFail() {
        // 因为spi 也是通过默认构造器加载类的，所以可以通过new 的方式来创建实例
        DefaultConsumeRetryProcessor consumeRetryProcessor = new DefaultConsumeRetryProcessor()
        def callback = {
            println(LocalDateTime.now().toString()+":重试方法回调：打印：" + it)
            throw new Exception("重试方法异常")
        } as Consumer
        queueWrapper.setRetryCallback(callback)
        consumeRetryProcessor.postConsume(invokerContent, queueWrapper)
        expect:
        1==1
    }

    void testPostConsumeWithExponentialDelayConfigSuccess() {
        MqConfig.setConfig(MqConfig.RETRY_CONSUME_DELAY_POLICY, RetryConst.DELAY_POLICY_EXPONENTIAL)
        // 因为spi 也是通过默认构造器加载类的，所以可以通过new 的方式来创建实例
        DefaultConsumeRetryProcessor consumeRetryProcessor = new DefaultConsumeRetryProcessor()
        consumeRetryProcessor.postConsume(invokerContent, queueWrapper)
        expect:
        1==1
    }

    void testPostConsumeWithExponentialDelayConfigFail() {
        MqConfig.setConfig(MqConfig.RETRY_CONSUME_DELAY_POLICY, RetryConst.DELAY_POLICY_EXPONENTIAL)
        MqConfig.setConfig(MqConfig.RETRY_CONSUME_IMMEDIATELY_COUNT, "5")
        // 因为spi 也是通过默认构造器加载类的，所以可以通过new 的方式来创建实例
        DefaultConsumeRetryProcessor consumeRetryProcessor = new DefaultConsumeRetryProcessor()
        def callback = {
            println(LocalDateTime.now().toString()+":重试方法回调：打印：" + it)
            throw new Exception("重试方法异常")
        } as Consumer
        queueWrapper.setRetryCallback(callback)
        consumeRetryProcessor.postConsume(invokerContent, queueWrapper)
        expect:
        1==1
    }
}
